package com.elot.controller.user;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.elot.controller.core.BaseController;
import com.elot.core.enums.RoleEnum;
import com.elot.core.types.DynamicArrayList;
import com.elot.core.utils.Converter;
import com.elot.dto.BankAccountDto;
import com.elot.dto.ReferenceDto;
import com.elot.dto.UserDto;
import com.elot.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController {

	@Autowired
	UserService userService;
	
	@RequestMapping(method = RequestMethod.GET)
	public String intro(Model model) {
		Collection<UserDto> list = userService.findAll();
		model.addAttribute("list", list);
		
		UserDto user = new UserDto();
		user.setFavoriteBankAccount(new DynamicArrayList<BankAccountDto>(BankAccountDto.class));
		user.setReferences(new DynamicArrayList<ReferenceDto>(ReferenceDto.class));
		model.addAttribute("user", user);
		
		model.addAttribute("roles", Converter.getKeyValuesByEnum(RoleEnum.values()));
		
		return "user/form";
	}

	@RequestMapping(value="/save", method=RequestMethod.POST)
    public String save(UserDto user, Model model) {
		userService.save(user);
        return intro(model);
    }

}
